<?php
require_once ("util.php");
require_once ("sql.php");
require_once ("user.php");

class ManageUsers {
	public function addUser($userName,$password, $firstName, $lastName, $email, $deletable = "NO", $modifiable = "NO") {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		// Add User
		$sql = SQLQuery::USER_ADD;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'sssssss', $userName,$password, $firstName, $lastName, $email, $deletable, $modifiable );
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function updateUser($userId, $userName,$password, $firstName, $lastName, $email, $deletable = "NO", $modifiable = "NO") {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		// Add User
		$sql = SQLQuery::USER_UPDATE;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'sssssssd', $userName,$password, $firstName, $lastName, $email, $deletable, $modifiable, $userId );
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function deleteUser($userId) {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		
		// Delete User
		$sql = SQLQuery::USER_DELETE;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'd', $userId );
		$status = $stmt->execute ();
		$stmt->close ();
		$conn->close ();
		return $status;
	}
	public function getUsers() {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		$sql = SQLQuery::USER_LIST;
		$stmt = $conn->prepare ( $sql );
		$status = $stmt->execute ();
		$users = array ();
		$result = $stmt->get_result ();
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$user = new User ();
				$user->setUserId ( $row ["UID"] );
				$user->setUserName ( $row ["USERNAME"] );
				$user->setFirstName ( $row ["FIRSTNAME"] );
				$user->setLastName ( $row ["LASTNAME"] );
				$user->setEmail ( $row ["EMAIL"] );
				$user->setCompanyName ( $row ["COMPANY"] );
				$user->setUnitName ( $row ["UNIT"] );
				$user->setRoleName ( $row ["ROLE"] );
				$user->setDeletable ( $row ["DELETABLE"] );
				$user->setModifiable ( $row ["MODIFIABLE"] );
				array_push ( $users, $user );
			}
		}
		$conn->close ();
		return $users;
	}
	public function getUser($userId) {
		$util = new DBUtil ();
		$conn = $util->getConnection ();
		$sql = SQLQuery::USER_FIND;
		$stmt = $conn->prepare ( $sql );
		$stmt->bind_param ( 'd', $userId );
		$status = $stmt->execute ();
		$user = NULL;
		$result = $stmt->get_result ();
		if ($result->num_rows > 0) {
			$user=new User();
			while ( $row = $result->fetch_array () ) {
				$user->setUserId ( $row ["UID"] );
				$user->setUserName ( $row ["USERNAME"] );
				$user->setFirstName ( $row ["FIRSTNAME"] );
				$user->setLastName ( $row ["LASTNAME"] );
				$user->setEmail ( $row ["EMAIL"] );
				$user->setCompanyName ( $row ["COMPANY"] );
				$user->setUnitName ( $row ["UNIT"] );
				$user->setRoleName ( $row ["ROLE"] );
				$user->setDeletable ( $row ["DELETABLE"] );
				$user->setModifiable ( $row ["MODIFIABLE"] );
			}
		}
		$conn->close ();
		return $user;
	}
	public function mapToRole($userId, $roleId) {
	}
	public function mapToUnit($userId, $unitId) {
	}
}
?>